import Vue from 'vue'
import VueRouter from 'vue-router'


//去掉路由重复报错的（百度的）
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}


Vue.use(VueRouter);

//黑马的路由懒加载,分组了的
// const Foo = () => import(/* webpackChunkName: "login_home_welcome" */ './Foo.vue')

 const router = new VueRouter({

  routes:[

    //路由1 
    {
      path:"/",
      redirect:"/login",
    },
    
    //路由2
    {
      path:"/login",
      component: ()=>import ("../components/login.vue"),

    },

    //路由3
    {
      path:"/home",
      component: ()=>import ("../components/Home.vue"),

      //重定向到子路由
      redirect:"/Welcome",

      //home的子路由
      children:[
        
        {
          path:"/Welcome",
          component: ()=>import ("../components/Welcome.vue"),
      
        },
        {
          path:"/users",
          component: ()=>import ("../components/user/Users.vue"),
      
        },
        {
          path:"/rights",
          component: ()=>import ("../components/power/Rights.vue"),
      
        },
        {
          path:"/roles",
          component: ()=>import ("../components/power/Roles.vue"),
      
        },
        {
          path:"/categories",
          component: ()=>import ("../components/goods/Ctae.vue"),
      
        },
        {
          path:"/params",
          component: ()=>import ("../components/goods/Params.vue"),
      
        },
        {
          path:"/goods",
          component: ()=>import ("../components/goods/Goods.vue"),
      
        },
        {
          path:"/goods/add",
          component: ()=>import ("../components/goods/Add.vue"),
      
        },
        {
          path:"/orders",
          component: ()=>import ("../components/order/Order.vue"),
      
        },
        {
          path:"/reports",
          component: ()=>import ("../components/report/Report.vue"),
      
        },
        

      ]

    }

  ]

})



//挂载路由导肮首位
router.beforeEach((to,form,next)=>{


  //to:将要访问的路径
  //form:代表从哪个路几个跳转进来
  //next:是一个函数,表示放行
  // console.log(to);

  //如果是登陆页面，直接放行
  if(to.path === '/login'){
    return next();
  }

  //如果是需要登陆之后才能访问的,设置权限
  //获取到token
  const tokenStr = window.sessionStorage.getItem('token');
  if(!tokenStr){

    // alert("请先登录!");
    // this.$message.error("登陆失败");
    return next({
      path:"/login"
    });
    
  }

  //登陆成功后调用这个函数进入下一个页面
  next();



})



export default router;